package com.tssf.car_manage.controller;

import com.tssf.car_manage.entity.BrandDTO;
import com.tssf.car_manage.entity.MonthlyIncome;
import com.tssf.car_manage.service.ChartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDate;
import java.util.List;

@Controller
@RequestMapping("/chart")
public class ChartController {
   @Autowired
    ChartService chartService;

    @GetMapping("/refresh")
    //统计数量（刷新按钮）
    public String refresh(@RequestParam(value = "year", required = false) Integer year, Model model){
        //统计客户数量
        int totalCustomers = chartService.customerCount();
        model.addAttribute("totalCustomers",totalCustomers);
        //统计车辆数量
        int totalCars = chartService.carCount();
        model.addAttribute("totalCars",totalCars);
        //统计维修记录数量
        int totalRepairs = chartService.repairCount();
        model.addAttribute("totalRepairs",totalRepairs);
       //防止刷新后数据丢失折线图不显示，刷新后同样显示折线图数据
        if(year == null ){
            //在刷新按钮时默认不输入年份，如果为空就获取当前年份赋值
            year = LocalDate.now().getYear();
            System.out.println(year);
        }
        //通过年份查询相应数据
        List<MonthlyIncome> monthlyIncomes = chartService.getIncomeByYear(year);
        System.out.println(monthlyIncomes);
        model.addAttribute("monthlyIncomes",monthlyIncomes);
        //向页面注入年份，用户可以具体看到是哪一年份的数据
        model.addAttribute("year", year);
        return "dashboard";
    }
    @GetMapping("/getIncomeByYear")
    public String getIncomeByYear(@RequestParam(value = "year", required = false) Integer year, Model model){
        //再次统计信息数量是因为，通过输入年份查询时会导致统计数量丢失，所以需要再次统计
        //统计客户数量
        int totalCustomers = chartService.customerCount();
        model.addAttribute("totalCustomers",totalCustomers);
        //统计车辆数量
        int totalCars = chartService.carCount();
        model.addAttribute("totalCars",totalCars);
        //统计维修记录数量
        int totalRepairs = chartService.repairCount();
        model.addAttribute("totalRepairs",totalRepairs);
        System.out.println(year);
        if(year == null ){
            year = LocalDate.now().getYear();
            System.out.println(year);
        }
        List<MonthlyIncome> monthlyIncomes = chartService.getIncomeByYear(year);
        System.out.println(monthlyIncomes);
        model.addAttribute("monthlyIncomes",monthlyIncomes);
        model.addAttribute("year", year);
        return "dashboard";
    }
    @GetMapping("/getBrandLim5")
    public String getBrandLim5(Model model){
        //统计车辆品牌数量
        int  carcount =  chartService.carCount();
        //统计维修数量
        int totalRepairs = chartService.repairCount();
        //统计前五维修记录品牌车辆数量
        List<BrandDTO> carLim5 = chartService.getCarLim5();
        //统计前五品牌车辆数量
       List<BrandDTO>  brandcount =  chartService.getBrandLim5();
       model.addAttribute("carcount",carcount);
       model.addAttribute("brandcount",brandcount);
       model.addAttribute("repaircount",carLim5);
        model.addAttribute("totalRepairs",totalRepairs);
       return "chart";
    }
}
